package com.gwz.context;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;

import com.gwz.model.JobBean;
import com.gwz.service.admin.QuartzBeanService;
import com.gwz.util.QuartzUtil;


/**
 * 随spring容器启动而启动执行初始化数据
 * @author panguixiang
 *
 */
@SuppressWarnings("serial")
public class InitContextBean extends HttpServlet {

    private static Logger logger = Logger.getLogger(InitContextBean.class);

    public void init() throws ServletException {
        initQuartz();//初始化quartz任务
    }

    /**
     * 初始化获得持久化的quartz 列表
     */
    private void initQuartz() {
        QuartzBeanService quartzBeanService = (QuartzBeanService) ApplicationContextHelper
            .getApplicationContext().getBean("quartzBeanService");
        List<JobBean> list = quartzBeanService.list();
        if (CollectionUtils.isNotEmpty(list)) {
            for (JobBean job : list) {
                try {
                    QuartzUtil.addJob(job);//开启quartz job；放入缓存
                } catch (Exception e) {
                    logger.error("初始化quartz job 信息异常==", e.fillInStackTrace());
                }
            }
        }
    }

}
